package com.example.forum.controller;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.config.AppConfig;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.model.User;
import com.example.forum.services.ArticleService;
import com.example.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Resource
    ArticleService articleService;

    /**
     * 构建首页的文章列表
     *
     * @param boardId
     * @return
     */
    @GetMapping("/getAllByBoardId")
    public AppResult<List<Article>> getAllByBoardId(HttpServletRequest request,Long boardId) {
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //boardId可以为空，但不能为零
        System.out.println(boardId);
        if (boardId == null) {
            List<Article> articleList = articleService.selectAll();
            return AppResult.success(articleList);
        } else if (boardId == 0) {
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        } else {
            List<Article> articleList = articleService.selectByBoardId(boardId);
            return AppResult.success(articleList);
        }
    }

    /**
     * 构建“我的列表”
     *
     * @param request
     * @param userId
     * @return
     */
    @GetMapping("/getAllByUserId")
    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request, Long userId) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        log.info("查询当前用户的文章");
        List<Article> articleList = null;
        if (userId == null || userId == user.getId()) {
            articleList = articleService.selectByUserId(user.getId());
            for(int i=0;i<articleList.size();i++){
                articleList.get(i).setOwn(true);
            }
        } else if (userId <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        } else {
            log.info("查询指定用户文章");
            articleList = articleService.selectByUserId(userId);
        }
        return AppResult.success(articleList);
    }


    /**
     * 发布帖子
     *
     * @param request
     * @param boardId
     * @param title
     * @param content
     * @return
     */
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            Long boardId, String title, String content) {
        //验证的登录信息
        HttpSession session = request.getSession(false);
        if (session == null) {
            //没有登陆
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //验证参数
        if (boardId == null || boardId == 0) {
            //非法参数
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (StringUtil.isEmpty(title) || StringUtil.isEmpty(content)) {
            //非法参数
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        Article article = new Article();
        article.setBoardId(boardId);
        article.setTitle(title);
        article.setContent(content);
        article.setUserId(user.getId());
        articleService.create(article);
        user.setArticleCount(user.getArticleCount()+1);
        session.setAttribute(AppConfig.USER_SESSION,user);
        return AppResult.success();
    }

    /**
     * 查看文章详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public AppResult<Article> getDetails(HttpServletRequest request, Long id) {
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        //id是文章的id
        Article article = articleService.selectDetailById(id);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()==article.getUserId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    //就不能使用对象来接受文章参数吗？啊！你看看这多费劲，还不能拓展！

    /**
     * 修改文章
     *
     * @param request
     * @param id
     * @param title
     * @param content
     * @return
     */
    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request, Long id, String title, String content) {
        HttpSession session = request.getSession(false);
        if (session == null) {
            //没有登陆
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        articleService.modify(id, title, content);
        return AppResult.success();
    }

    /**
     * 点赞文章
     *
     * @param request
     * @param id
     * @return
     */
    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request, Long id) {
        //验证的登录信息
        HttpSession session = request.getSession(false);
        if (session == null) {
            //没有登陆
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        if (id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        articleService.thumbsUpById(id);
        return AppResult.success();
    }

    /**
     * 删除文章
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public AppResult deleteById(HttpServletRequest request, Long id) {
        //验证的登录信息
        HttpSession session = request.getSession(false);
        if (session == null) {
            //没有登陆
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        if (id == null || id <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        articleService.deleteById(id);
        return AppResult.success();
    }

    /**
     * 构建评论列表
     *
     * @param articleId
     * @param request
     * @return
     */
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(Long articleId, HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if(session==null){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }
        if (articleId == null || articleId <= 0) {
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        List<ArticleReply> articleReplies = articleService.selectReplyByArticleId(articleId);
        return AppResult.success(articleReplies);
    }

}
